package com.xiyoumobile.dengtuo.flowershop;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import common.ISendData;
import controller.UserLoginController;
import model.RequestModel;

public class RegisterActivity extends Activity {


    UserLoginController userLoginController;

    //键盘
    InputMethodManager imm;
    //返回键
    ImageButton imageButton;

    EditText password;
    EditText phone;
    EditText repassword;
    EditText email;
    EditText name;

    Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userLoginController = new UserLoginController(RegisterActivity.this);
        findView();
    }

    private void findView() {

        imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imageButton = (ImageButton) findViewById(R.id.title_bar_back);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });


        password = (EditText) findViewById(R.id.password);
        phone = (EditText) findViewById(R.id.phone);
        repassword = (EditText) findViewById(R.id.repassword);
        email = (EditText) findViewById(R.id.email);
        name = (EditText) findViewById(R.id.name);
        register = (Button) findViewById(R.id.register);
        register.setOnClickListener(new ButtonOnClickLisenter());
    }

    private class ButtonOnClickLisenter implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (imm != null) {
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
            }

            //判断网络

            //判断条件
            if (null == name.getText().toString() || name.getText().toString().isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入昵称", Toast.LENGTH_SHORT).show();
                return;
            }
            //判断条件
            if (null == phone.getText().toString() || phone.getText().toString().isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                return;
            }
            //判断条件
            if (null == email.getText().toString() || email.getText().toString().isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show();
                return;
            }
            //判断条件
            if (null == password.getText().toString() || password.getText().toString().isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }

            //判断条件
            if (null == repassword.getText().toString() || repassword.getText().toString().isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入确认密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!password.getText().toString().equals(repassword.getText().toString())) {
                Toast.makeText(RegisterActivity.this, "两次密码填写不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            //开始请求
            if (null == userLoginController) {
                userLoginController = new UserLoginController(RegisterActivity.this);
            }


            userLoginController.register(phone.getText().toString(), password.getText().toString(), email.getText().toString(),
                    name.getText().toString(), new ISendData() {
                        @Override
                        public void sendData(Object o) {
                            RequestModel<Object> info = (RequestModel<Object>) o;
                            if(info.getStatus()  == 0){
                                //注册成功
                            }else{

                            }
                        }
                    });


        }
    }
}
